//
//  GridBluetoothTitleTableViewCell.swift
//  grid-ios-app
//
//  Created by wondertek on 2024/4/1.
//

import UIKit
import SnapKit

internal protocol GridBluetoothTitleTableViewCellDelegate: NSObjectProtocol {
    
    /// 点击链接
    func gridBluetoothTitleTableViewCellLink(model :GridBluethoothCellModel?)
}

class GridBluetoothTitleTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupLayouts()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    internal weak var delegate: GridBluetoothTitleTableViewCellDelegate?

    var cellModel :GridBluethoothCellModel? {
        didSet {
            titleLabel.text = cellModel?.peripheral.name ?? "未命名"
            messageLabel.text = cellModel?.peripheral.identifier.uuidString ?? ""
            if cellModel?.state == .noLinking {
                linkBtn.setTitle(NSLocalizedString("连接", tableName: "GridLocalizable", comment: ""), for: .normal)
                linkBtn.isHidden = false

            }else {
                linkBtn.setTitle(NSLocalizedString("已连接", tableName: "GridLocalizable", comment: ""), for: .normal)
                linkBtn.isHidden = true
            }
        }
    }
    
    func setupLayouts() {
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(6)
            make.right.equalToSuperview().offset(-100)
            make.height.equalTo(20)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalTo(titleLabel.snp.right)
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.height.equalTo(20)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        linkBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-12)
            make.centerY.equalToSuperview()
            make.width.equalTo(60)
            make.height.equalTo(20)
        }
    }
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        contentView.addSubview(view)
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        label.backgroundColor = .clear
        contentView.addSubview(label)
        return label
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(10)
        label.text = ""
        contentView.addSubview(label)
        return label
    }()
    
    private lazy var linkBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("连接", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(10)
        button.addTarget(self, action: #selector(onClickButton), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 10
        contentView.addSubview(button)
        return button
    }()
    
    @objc func onClickButton() {
        print("更新逻辑")
        if cellModel?.state == .noLinking {
            delegate?.gridBluetoothTitleTableViewCellLink(model: cellModel)
        }
    }
    
    
}

// 顶部
class GridBluetoothBottomView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        
        firstBtn.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(40)
            make.right.equalToSuperview().offset(-20)
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(40)
        }
        
        firstBtn1.snp.makeConstraints { make in
            make.top.equalTo(firstBtn.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-20)
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(40)
        }
        
        firstBtn2.snp.makeConstraints { make in
            make.top.equalTo(firstBtn1.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-20)
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(40)
        }
    }
    
    lazy var firstBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("按钮1", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(18, bold: true)
        button.addTarget(target, action: #selector(self.onClickButton(_:)), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 20
        addSubview(button)
        return button
    }()
    
    lazy var firstBtn1: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("按钮1", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(18, bold: true)
        button.addTarget(target, action: #selector(self.onClickButton(_:)), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 20
        addSubview(button)
        return button
    }()
    
    lazy var firstBtn2: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("按钮1", tableName: "GridLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(18, bold: true)
        button.addTarget(target, action: #selector(self.onClickButton(_:)), for: .touchUpInside)
        button.backgroundColor  = UIColor(hex6: 0x1E90FF)
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 20
        addSubview(button)
        return button
    }()
    
    
    @objc func onClickButton(_ sender: UIButton) {
        print("onClickButton")
        if sender == firstBtn {
            
        }
       
    }
}
